<?php
namespace jy_jindoufee;
defined('BY_JYA') or exit('error');
/*
	分佣佣金
	
*/
class commission extends \plugin{
	function addorder($order){
		return true;
	}
	/*
		生成佣金
	*/
	function create($order){
		$is_open=intval($this->setting['is_open']);
		if(!$is_open){
			return;
		}
		$this->order=$order;
		$member=pdo_get('core_members',array('id'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),array('fromuser'));
		$myagent=pdo_get('jy_jindoufee_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
		if(!$myagent['mid']){
			$this->model('member')->register(array('mid'=>$order['mid']));
			$myagent=pdo_get('jy_jindoufee_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
		}
		if($myagent['is_agent']){
			pdo_update('jy_jindoufee_member',array('buyfee +='=>$order['price']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
			$this->model('member')->update_agent_level($order['mid'],$order['id']);
		}
		$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$this->order['mid'],
				'agent_id'=>$member['fromuser'],
				'ordersn'=>$this->order['ordersn'],
				'price'=>$this->order['price'],
				'plugin_name'=>$this->order['plugin_name']?:'',
				'commission_price'=>$this->order['price'],
				'commission_money'=>0,
				'ggprice'=>0,
				'ggorderprice'=>0,
				'ispay'=>1,
				'status'=>1,
				'createtime'=>TIMESTAMP,
		);
		$fromuser=$myagent['mid'];
		for($i=0;$i<3;$i++){
			if(!$fromuser){
				break;
			}
			if($i){
				$member=pdo_get('core_members',array('id'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),array('fromuser'));
				$agent=pdo_get('jy_jindoufee_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$fromuser));
			}else{
				$agent=$myagent;
			}
			$fromuser=$member['fromuser'];
			if($agent['is_agent']){
				if($agent['level_id']){
					$level=pdo_get('jy_jindoufee_level',array('id'=>$agent['level_id'],'uniacid'=>$_SESSION['uniacid']));
				}
				$agent['agent_id']=$fromuser;
				$this->create_commission($agent,$i,$level);
			}
		}
		//送金豆
		$this->create_jindou();
		//生成分佣订单
		$indata=$this->commission_order;
		$this->insert('order',$indata);
		return true;
	}
	function create_jindou(){
		$levels=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']));
		foreach($levels as $level){
			$level_id=$level['id'];
			$jindou_money=0;
			$order_price=$this->commission_order['commission_price'];
			$jindou_rate=$level['jindou_rate'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
			);
			$this->needorder=false;
			$goodsids=';';
			if($this->setting['get_buyordernum']!=1){
				foreach($this->order['goods'] as $goods){
					if(!$goods['id']){
						$this->needorder=true;
						continue;	
					}
					$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'total_price');
					$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
					if(!$goods_commission['id']){
						$this->needorder=true;
						continue;
					}
					$order_price=$order_price-$total_price;
					if($goods_commission['ignore']){
						continue;
					}
					$goods_commission['commissions']=$goods_commission['commissions']?json_decode($goods_commission['commissions'],true):array();
					foreach($goods_commission['commissions'] as $gc){
						if($gc['level_id']!=$level_id){
							continue;
						}else{
							$goods_jindou_rate=$gc['jindou_rate'];
							break;
						}
					}
					if(substr($goods_logjf_rate,-1)=='%'){
						$jindoumoney=($total_price*substr($goods_jindou_rate,0,-1))/100;
					}else{
						$jindoumoney=$goods_jindou_rate*$goods['total'];
					}
					$jindou_money +=$jindoumoney;
				}
			}
			if($this->needorder || count($this->order['goods'])<=0){
				if(substr($jindou_rate,-1)=='%'){
					$jindoumoney=($order_price*substr($jindou_rate,0,-1))/100;
				}else{
					$jindoumoney=$jindou_rate*$goods['total'];
				}
				$jindou_money +=$jindoumoney;
			}
			if($jindou_money>0){
				$agents=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'level_id'=>$level_id,'is_agent'=>'1','stop'=>0));
				foreach($agents as $mv){
					$feelog=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mv['mid'],
						'ordersn'=>$this->order['ordersn'],
						'fee'=>$jindou_money,
						'type'=>11,
						'status'=>2,
						'createtime'=>TIMESTAMP,
					);
					$this->insert('feelog',$feelog);
					model('core/member')->member_credit($mv['mid'],'credit3',$jindou_money,true,'jy_jindoufee','自购送金豆');
				}
			}
		}
		return 0;
	}
	function create_commission($member,$commission_level=0,$level=array()){
		if($member && $member['is_agent'] && !$member['stop']){
			//相应会员存在且是分销商且状态正常			
			if(!$member['level_id']){
				//获取默认等级佣金比例
				$level=$this->plugin_setting('default_level');
				$level['level']=0;
				$level['id']=0;
				$level_id=0;
			}else{
				$level_id=$level['id'];
			}
			$red_money=0;
			$jian_money=0;
			$sign_money=0;
			$loggg_money=0;
			$lognl_money=0;
			$logjf_money=0;
			$jindou_money=0;
			$ggprice=0;
			$order_price=$this->commission_order['commission_price'];
			$red_rate=$level['red_rate'];
			$jian_rate=$level['jian_rate'];
			$sign_rate=$level['sign_rate'];
			$loggg_rate=$level['loggg_rate'];
			$lognl_rate=$level['lognl_rate'];
			$logjf_rate=$level['logjf_rate'];
			$sfgg_num=$level['sfgg_num'];
			$jindou_rate=$level['jindou_rate'];
			$remark=array(
				'level_id'=>$level_id,
				'commission_level'=>$commission_level,
				'order_price'=>$order_price,
			);
			//先检查是否存在商品独立佣金规则，如果存在则对订单相应商品部分金额单独计算佣金
			//非按笔计算
			if(!$commission_level){
				$downmids=pdo_getall('core_members',array('fromuser'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),'id','id');
				if($downmids){
					$downmids=array_keys($downmids);
				}
			}
			$this->needorder=false;
			$goodsids=';';
			if($this->setting['get_buyordernum']!=1){
				foreach($this->order['goods'] as $goods){
					if(!$goods['id']){
						$this->needorder=true;
						continue;	
					}
					$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']),'total_price');
					$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']));
					if(!$goods_commission['id']){
						$this->needorder=true;
						continue;
					}
					$order_price=$order_price-$total_price;
					if($goods_commission['ignore']){
						continue;
					}
					if($downmids){
						$isbuynum=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$downmids,'goodsid'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),'total');
					}
					$goods_commission['commissions']=$goods_commission['commissions']?json_decode($goods_commission['commissions'],true):array();
					foreach($goods_commission['commissions'] as $gc){
						if($gc['level_id']!=$level_id){
							continue;
						}else{
							$goods_sfgg_num=$gc['sfgg_num'];
							$goods_red_rate=$gc['red_rate'];
							$goods_jian_rate=$gc['jian_rate'];
							$goods_sign_rate=$gc['sign_rate'];
							$goods_loggg_rate=$gc['loggg_rate'];
							$goods_lognl_rate=$gc['lognl_rate'];
							$goods_logjf_rate=$gc['logjf_rate'];
							$goods_jindou_rate=$gc['jindou_rate'];
							break;
						}
					}
					if($goods_sfgg_num && $total_price && !$commission_level){
						$ggprice +=$goods_sfgg_num*$total_price/100;
					}
					if(substr($goods_red_rate,-1)=='%'){
						$goods_red_money=($total_price*substr($goods_red_rate,0,-1))/100;
						$red_money+=$goods_red_money;
					}else{
						$goods_red_money=$goods_red_rate*$goods['total'];
						$red_money+=$goods_red_money;
					}
					if(substr($goods_jian_rate,-1)=='%'){
						$goods_ld_money=($total_price*substr($goods_jian_rate,0,-1))/100;
						$jian_money+=$goods_ld_money;
					}else{
						$goods_ld_money=$goods_jian_rate*$goods['total'];
						$jian_money+=$goods_ld_money;
					}
					if(substr($goods_sign_rate,-1)=='%'){
						$goods_jie_money=($total_price*substr($goods_sign_rate,0,-1))/100;
						$sign_money+=$goods_jie_money;
					}else{
						$goods_jie_money=$goods_sign_rate*$goods['total'];
						$sign_money+=$goods_jie_money;
					}
					if(substr($goods_loggg_rate,-1)=='%'){
						$logmoney=($total_price*substr($goods_loggg_rate,0,-1))/100;
					}else{
						$logmoney=$goods_loggg_rate*$goods['total'];
					}
					$loggg_money +=$logmoney;
					if(substr($goods_lognl_rate,-1)=='%'){
						$logmoney=($total_price*substr($goods_lognl_rate,0,-1))/100;
					}else{
						$logmoney=$goods_lognl_rate*$goods['total'];
					}
					$lognl_money +=$logmoney;
					if(substr($goods_logjf_rate,-1)=='%'){
						$logmoney=($total_price*substr($goods_logjf_rate,0,-1))/100;
					}else{
						$logmoney=$goods_logjf_rate*$goods['total'];
					}
					$logjf_money +=$logmoney;
					$goodsids .=$goods['goodsid'].';';
					$remark['goods_commission'][]=array('ordergoodsid'=>$goods['id'],'goods_id'=>$goods['goodsid'],'total_price'=>$total_price,'total'=>$goods['total'],'commission_per'=>$goods_red_rate,'commission_money'=>$goods_red_rate);
				}
			}
			$isgg_money=pdo_getsum('jy_jindoufee_feelog',array('type'=>1,'mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),'fee');
			$lastggmoney=$loggg_rate-$isgg_money;
			$loggg_money=$lastggmoney>$loggg_money?$loggg_money:$lastggmoney;
			$isnl_money=pdo_getsum('jy_jindoufee_feelog',array('type'=>2,'mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),'fee');
			$lastnlmoney=$lognl_rate-$isnl_money;
			//file_put_contents(ROOT_D.'/1.TXT',$lastnlmoney.'~isnl_money~'.$isnl_money."\n",FILE_APPEND);
			$lognl_money=$lastnlmoney>$lognl_money?$lognl_money:$lastnlmoney;
			$isjf_money=pdo_getsum('jy_jindoufee_feelog',array('type'=>3,'mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),'fee');
			$lastjfmoney=$logjf_rate-$isjf_money;
			$logjf_money=$lastjfmoney>$logjf_money?$logjf_money:$lastjfmoney;
			if($this->needorder || count($this->order['goods'])<=0){
				if($sfgg_num && $order_price && !$commission_level){
					$ggorderprice =$order_price;
				}
				if(substr($red_rate,-1)=='%'){
					$red_money=intval($order_price*substr($red_rate,0,-1))/100;
				}else{
					$red_money=$red_rate;
				}
				if(substr($jian_rate,-1)=='%'){
					$goods_ld_money=($order_price*substr($jian_rate,0,-1))/100;
					$jian_money+=$goods_ld_money;
				}else{
					$goods_ld_money=$jian_rate*$goods['total'];
					$jian_money+=$goods_ld_money;
				}
				if(substr($sign_rate,-1)=='%'){
					$goods_jie_money=($order_price*substr($sign_rate,0,-1))/100;
					$sign_money+=$goods_jie_money;
				}else{
					$goods_jie_money=$sign_rate*$goods['total'];
					$sign_money+=$goods_jie_money;
				}
			}
			if(!$commission_level){
				$this->commission_order['ggprice']=$ggprice;
				$this->commission_order['ggorderprice']=$ggorderprice;
			}
			//自购送广告值
			if($loggg_money>0 && !$commission_level){
				$feelog=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'ordersn'=>$this->order['ordersn'],
						'fee'=>$loggg_money,
						'type'=>1,
						'goodsids'=>$goodsids,
						'status'=>$this->setting['isbuy_jiesuan']?2:0,
						'createtime'=>TIMESTAMP,
				);
				$this->insert('feelog',$feelog);
				if($this->setting['isbuy_jiesuan']){
					pdo_update('jy_jindoufee_member',array('ggfee +='=>$loggg_money),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
			//自购送能量值
			if($lognl_money>0 && !$commission_level){
				$feelog=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'ordersn'=>$this->order['ordersn'],
						'fee'=>$lognl_money,
						'type'=>2,
						'goodsids'=>$goodsids,
						'status'=>$this->setting['isbuy_jiesuan']?2:0,
						'createtime'=>TIMESTAMP,
				);
				$this->insert('feelog',$feelog);
				if($this->setting['isbuy_jiesuan']){
					pdo_update('jy_jindoufee_member',array('nlfee +='=>$lognl_money),array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
				}
			}
			//自购送积分
			if($logjf_money>0 && !$commission_level){
				$feelog=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'ordersn'=>$this->order['ordersn'],
						'fee'=>$logjf_money,
						'type'=>3,
						'goodsids'=>$goodsids,
						'status'=>$this->setting['isbuy_jiesuan']?2:0,
						'createtime'=>TIMESTAMP,
				);
				$this->insert('feelog',$feelog);
				if($this->setting['isbuy_jiesuan']){
					model('core/member')->member_credit($member['mid'],'credit1',$logjf_money,true,'jy_jindoufee','自购送积分');
				}
			}
			//直推收益
			if($red_money>0 && $commission_level==1){
				$this->commission_order['commission_money']+=$red_money;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['team_mid']?:$member['mid'],
						'ordersn'=>$this->order['ordersn'],
						'money'=>$red_money,
						'type'=>1,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>$this->setting['isbuy_jiesuan']?2:0,
				);
				$this->insert('commission',$new_commission);
				if($this->setting['isbuy_jiesuan']){
					model('core/member')->member_credit($member['team_mid']?:$member['mid'],'credit3',$red_money,true,'jy_jindoufee','直推收益金豆');
				}
			}
			//间推收益
			if($jian_money>0 && $commission_level==2){
				$this->commission_order['commission_money']+=$jian_money;
				$new_commission=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$member['mid'],
						'ordersn'=>$this->order['ordersn'],
						'money'=>$jian_money,
						'type'=>2,
						'remark'=>ijson_encode($remark),
						'createtime'=>TIMESTAMP,
						'status'=>$this->setting['isbuy_jiesuan']?2:0,
				);
				$this->insert('commission',$new_commission);
				if($this->setting['isbuy_jiesuan']){
					model('core/member')->member_credit($member['mid'],'credit3',$jian_money,true,'jy_jindoufee','间推收益金豆');
				}
			}
			//直推签到奖励
			if($sign_money>0 && $commission_level==1){
				//$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
				//$istype3=pdo_count('jy_jindoufee_commission',array('createtime >'=>$daytime,'type'=>3,'ordermid'=>$this->order['mid'],'mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
				//if(!$istype3){
					$this->commission_order['commission_money']+=$sign_money;
					if(substr($level['signsf_rate'],-1)=='%'){
						$onemoney=($sign_money*substr($level['signsf_rate'],0,-1))/100;
					}else{
						$onemoney=$level['signsf_rate'];
					}
					$new_commission=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$member['mid'],
							'ordersn'=>$this->order['ordersn'],
							'ordermid'=>$this->order['mid'],
							'money'=>$sign_money,
							'onemoney'=>$onemoney,
							'lastmoney'=>$sign_money,
							'type'=>3,
							'remark'=>ijson_encode($remark),
							'createtime'=>TIMESTAMP,
							'status'=>$this->setting['isbuy_jiesuan']?1:0,
					);
					$this->insert('commission',$new_commission);
				//}
			}
		}
		return 0;
	}
	public function order_complete($order){
		$this->update('order',array('status'=>3),array('ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		$list=$this->getall('feelog',array('type'=>array(1,2,3),'status'=>0,'ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		foreach($list as $item){
			if($item['type']==3){
				model('core/member')->member_credit($item['mid'],'credit1',$item['fee'],true,'jy_jindoufee','自购送积分');
			}elseif($item['type']==2){
				pdo_update('jy_jindoufee_member',array('nlfee +='=>$item['fee']),array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			}elseif($item['type']==1){
				pdo_update('jy_jindoufee_member',array('ggfee +='=>$item['fee']),array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		$list=$this->getall('commission',array('type'=>array(1,2),'status'=>0,'ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		foreach($list as $item){
			model('core/member')->member_credit($item['mid'],'credit3',$item['money'],true,'jy_jindoufee','收益金豆');
		}
		$this->update('commission',array('status'=>2,'complete_time'=>TIMESTAMP),array('ordersn'=>$order['ordersn'],'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		$this->update('feelog',array('status'=>2),array('type'=>array(1,2,3),'status'=>0,'ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
		return 0;
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='downline'){
			if(!$post['wechat']){
				exi('请先填写微信号！','error');
			}
			if(!$post['mobile']){
				exi('请先填写联系电话！','error');
			}
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
			$typetitle='支付宝';
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
			$typetitle='银行卡';
		}
		//获取可结算佣金
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1);
		if($this->setting['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		$money=pdo_getsum('jy_commission_commission',$params,'money');
		if($money<=0){
			exi('没有可结算的佣金！','error');
		}
		$commission_num=$this->count('commission',$params,'id');
		//结算最低佣金检测
		if($money<$this->setting['balance_money']){
			exi('可结算佣金太少，无法结算！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'wechat'=>$post['wechat'],
			'mobile'=>$post['mobile'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->setting['balance_per'] && ($post['pay_type']!='credit' || !$this->setting['balance_credit'])){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->setting['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$update=array(
			'status'=>2,
			'balancesn'=>$new_balance['balancesn'],
		);
		$this->update('commission',$update,$params);
		if($post['pay_type']=='credit'){
			$typetitle='余额';
		}
		if($post['pay_type']=='wechat'){
			$typetitle='微信';
		}
		$this->model('notice')->notice_fenxiao_tixian($mid,$money,$typetitle);
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
		return true;
		/*$update=array(
			'status'=>1,
			'balancesn'=>'',
		);
		$balance=$this->get('balance',array('id'=>$balance_id),array('mid','balancesn'));
		if($balance['balancesn']){
			$this->update('commission',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$balance['mid'],'balancesn'=>$balance['balancesn']));
		}*/
		return true;
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type='',$wxgid='',$pay_trade_no=''){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			//exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn'],$wxgid);
		$update=array(
			'status'=>3,
			'pay_trade_no'=>$trade_no=='pay_trade_no'?$pay_trade_no:$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		if($pay_type){
			$update['pay_type']=$pay_type;
		}
		$this->update('balance',$update,array('id'=>$balance['id']));
		$this->model('notice')->notice_fenxiao_dakuan($balance['mid'],$balance['final_money']);
		return true;
	}
	/*
		线下打款
	*/
	protected function pay_balance_by_downline(){
		return 'pay_trade_no';
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,'jy_commission','分销佣金打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn,$wxgid=''){
		if($wxgid){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$wxgid,'uniacid'=>$_SESSION['uniacid']),array('appid','mchid','paykey'));
		}
		$params=array(
			'appid'=>$wxapp['appid']?:'',
			'mch_id'=>$wxapp['mchid']?:'',
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'wxgid'=>$wxgid,
			'remark'=>'代理商佣金发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		if(!$res['payment_no'] && !$res['batch_id']){
			exi('打款到微信零钱失败！','error');
		}
		return $res['payment_no']?:$res['batch_id'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		return 'pay_trade_no';
		exi('暂不支持打款到银行账户！','error');
	}
}